#Fig_1_2_5

qh_dyads <-read_csv("QH_dyads.csv")
qh_conflict <-read_csv("QH_conflict.csv")
drop_case<-read_xlsx("civil wars essay 25th.xlsx")

qh_dyads %>%
  filter(is.na(coup_indicator)) %>%
  filter(type_of_conflict >=3) %>%
  filter(conflict_id %in% drop_case$conflict_id) %>%
  mutate(recurrence = ifelse(dyadepisode > 1, 1, 0)) %>%
  group_by(year, recurrence) %>%
  mutate(count=n()) %>%
  dplyr::select(year, count, recurrence) %>%
  distinct() %>%
  ungroup() %>%
  complete(year=1946:2019, recurrence, fill = list(count=0)) %>%
  group_by(recurrence) %>%
  mutate(roll = rollmean(count, 5, fill = list(NA, NULL, NA))) %>%
  pivot_longer(cols=c(count, roll)) %>%
  filter(name=="roll") %>%
  ggplot(aes(year, value, color=
               factor(recurrence, levels=c(0,1), labels=c("Active Dyads in Episode 1", "Active Dyads in Episode >= 2")))) +
  geom_line(cex=1)+
  labs(color="Type of Dyad", x="Year", y="Number of Active Dyads")+
  theme_bw()+
  theme(legend.position="bottom")+
  guides(color=guide_legend(title.position="top", title.hjust = .5))
  
ggsave("Fig1.png", path=imdir, width=6, height = 6, units="in")

qh_conflict %>%
  filter(is.na(coup_indicator)) %>%
  filter(type_of_conflict >=3) %>%
  filter(conflict_id %in% drop_case$conflict_id) %>%
  mutate(recurrence = ifelse(conflictepisode > 1, 1, 0)) %>%
  group_by(year, recurrence) %>%
  mutate(count=n()) %>%
  dplyr::select(year, count, recurrence) %>%
  distinct() %>%
  ungroup() %>%
  complete(year=1946:2019, recurrence, fill = list(count=0)) %>%
  group_by(recurrence) %>%
  mutate(roll = rollmean(count, 5, fill = list(NA, NULL, NA))) %>%
  pivot_longer(cols=c(count, roll)) %>%
  filter(name=="roll") %>%
  ggplot(aes(year, value, color=
               factor(recurrence, levels=c(0,1), labels=c("Active Conflicts in Episode 1", "Active Conflicts in Episode >= 2")))) +
  geom_line(cex=1)+
  labs(color="Type of Conflict", x="Year", y="Number of Active Conflicts")+
  theme_bw()+
  theme(legend.position="bottom")+
  guides(color=guide_legend(title.position="top", title.hjust = .5))

ggsave("Fig2.png", path=imdir, width=6, height = 6, units="in")

qh_conflict %>%
  filter(is.na(coup_indicator)) %>%
  filter(type_of_conflict >=3) %>%
  filter(conflict_id %in% drop_case$conflict_id) %>%
  filter(outcome %in% c(1,3,4)) %>%
  group_by(year, outcome) %>%
  mutate(count=n()) %>%
  dplyr::select(outcome, year, count) %>%
  distinct() %>%
  ungroup() %>%
  complete(year=1946:2019, outcome, fill = list(count=0)) %>%
  ggplot(aes(year, count, color=
               factor(outcome, levels = c(1,3,4), labels= c("Peace Agreement", "Government Victory", "Rebel Victory")))) +
  geom_smooth(method="glm", formula = y~poly(x-1945,3, raw=T), method.args=list(family="poisson"), se=F)+
  labs(color="Type of Termination", x="Year", y="Number of Terminations")+
  theme_bw()+
  theme(legend.position="bottom")+
  guides(color=guide_legend(title.position="top", title.hjust = .5))


ggsave("Fig5.png", path=imdir, width=6, height = 4, units="in")
